package cn.silver.module.configuration.service.impl;

import cn.silver.framework.core.constant.CacheNames;
import cn.silver.framework.core.utils.StringUtils;
import cn.silver.framework.mybatis.annotation.TableField;
import cn.silver.framework.mybatis.service.impl.BusServiceImpl;
import cn.silver.framework.redis.utils.RedisUtils;
import cn.silver.module.configuration.bean.DictItemBean;
import cn.silver.module.configuration.domain.SysDictItem;
import cn.silver.module.configuration.mapper.SysDictItemMapper;
import cn.silver.module.configuration.param.DictItemParam;
import cn.silver.module.configuration.service.ISysDictItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 字典 业务层处理
 *
 * @author Silver Zhou
 */
@Service
@RequiredArgsConstructor
public class SysDictItemServiceImpl extends BusServiceImpl<SysDictItemMapper, SysDictItem, DictItemParam, DictItemBean> implements ISysDictItemService {

    @Override
    protected Long selectExist(DictItemBean model) {
        QueryWrapper<SysDictItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(StringUtils.isNotBlank(model.getId()), "id", model.getId());
        queryWrapper.eq("type", model.getType());
        queryWrapper.and(wrapper -> {
            wrapper.eq(org.apache.commons.lang3.StringUtils.isNotBlank(model.getCode()), "code", model.getCode());
            wrapper.or().eq(org.apache.commons.lang3.StringUtils.isNotBlank(model.getName()), "name", model.getName());
            List<Field> fields = Arrays.stream(model.getClass().getDeclaredFields()).filter(item -> item.isAnnotationPresent(TableField.class)).toList();
            for (Field field : fields) {
                TableField tableField = field.getAnnotation(TableField.class);
                if (tableField.unique()) {
                    wrapper.or().eq(org.apache.commons.lang3.StringUtils.isNotBlank(field.getName()), tableField.name(), model.getId());
                }
            }
        });
        return this.baseMapper.selectCount(queryWrapper);
    }

    /**
     * 新增保存字典数据信息
     *
     * @param model 字典数据信息
     * @return 结果
     */
    @Override
//    @CachePut(cacheNames = CacheNames.SYS_DICT, key = "#model.type")
    public boolean insert(DictItemBean model) {
        return super.insert(model);
    }

    /**
     * 修改保存字典数据信息
     *
     * @param model 字典数据信息
     * @return 结果
     */
    @Override
//    @CachePut(cacheNames = CacheNames.SYS_DICT, key = "#model.type")
    public boolean update(DictItemBean model) {
        return super.update(model);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param ids 需要删除的字典数据ID
     */
    @Override
    public void deleteBatch(Collection<String> ids) {
        List<DictItemBean> models = this.baseMapper.selectVoBatchIds(ids);
        this.baseMapper.deleteByIds(ids);
        List<String> codes = models.stream().map(item -> CacheNames.SYS_DICT + item.getType()).distinct().toList();
        RedisUtils.deleteObject(codes);
    }
}
